#!/usr/bin/env groovy
package com.yidao.www

def embedHexMerge(target, artifactDir){
    def bootFile = powershell(returnStdout: true, script: '$a = dir BOOT\\*.zip; $a.name').trim()
    println "bootZip: ${target}\\${bootFile}"
    println "artifactDir: ${artifactDir}"
    def artifactName = artifactDir
    unzip charset: "GBK", dir: "${target}", glob: "", zipFile: "${target}\\${bootFile}"
    def bootHexFile = powershell(returnStdout: true, script: '$a = dir BOOT\\*BOOT\\*.hex; $a.name').trim()
    def bootDir = powershell(returnStdout: true, script: '$a = dir BOOT\\*BOOT; $a.name').trim()
    def appHexFile = powershell(returnStdout: true, script: "\$a = dir ${artifactName}\\*CTRL\\*.hex; \$a.name").trim()
    def appDir = powershell(returnStdout: true, script: "\$a = dir ${artifactName}\\*CTRL; \$a.name").trim()
    def hexFlagFile = powershell(returnStdout: true, script: "\$a = dir ${artifactName}\\*CTRL\\*.hexflag; \$a.name").trim()

    println "=======================HEX INFO======================="
    println "bootFile: " + bootFile
    println "bootHexFile: " + bootHexFile
    println "bootDir: " + bootDir
    println "appHexFile: " + appHexFile
    println "appDir: " + appDir
    println "=======================HEX INFO======================="

    bat "xcopy ${target}\\${bootDir}\\${bootHexFile} ${artifactName}\\${appDir}"
    bat "python %hexMergeScript% ${artifactName}\\${appDir}\\${bootHexFile} ${artifactName}\\${appDir}\\${hexFlagFile} ${artifactName}\\${appDir}\\${appHexFile} ${artifactName}\\${appDir}\\${appHexFile}"
    bat "del ${artifactName}\\${appDir}\\${bootHexFile}"
    bat "del ${artifactName}\\${appDir}\\${hexFlagFile}"
}

def embedHexMergeM100(target, artifactDir){
    def bootFile = powershell(returnStdout: true, script: '$a = dir BOOT\\*.zip; $a.name').trim()
    println "bootZip: ${target}\\${bootFile}"
    println "artifactDir: ${artifactDir}"
    def artifactName = artifactDir
    unzip charset: "GBK", dir: "${target}", glob: "", zipFile: "${target}\\${bootFile}"
    def bootHexFile = powershell(returnStdout: true, script: '$a = dir BOOT\\*BOOT\\*.hex; $a.name').trim()
    def bootDir = powershell(returnStdout: true, script: '$a = dir BOOT\\*BOOT; $a.name').trim()
    def appHexFile = powershell(returnStdout: true, script: "\$a = dir ${artifactName}\\*CTRL*\\*.hex; \$a.name").trim()
    def appDir = powershell(returnStdout: true, script: "\$a = dir ${artifactName}\\*CTRL*; \$a.name").trim()
    def hexFlagFile = powershell(returnStdout: true, script: "\$a = dir ${artifactName}\\*CTRL*\\*.hexflag; \$a.name").trim()

    println "=======================HEX INFO======================="
    println "bootFile: " + bootFile
    println "bootHexFile: " + bootHexFile
    println "bootDir: " + bootDir
    println "appHexFile: " + appHexFile
    println "appDir: " + appDir
    println "=======================HEX INFO======================="

    bat "xcopy ${target}\\${bootDir}\\${bootHexFile} ${artifactName}\\${appDir}"
    bat "python %hexMergeScript% ${artifactName}\\${appDir}\\${bootHexFile} ${artifactName}\\${appDir}\\${hexFlagFile} ${artifactName}\\${appDir}\\${appHexFile} ${artifactName}\\${appDir}\\${appHexFile}"
    bat "del ${artifactName}\\${appDir}\\${bootHexFile}"
    bat "del ${artifactName}\\${appDir}\\${hexFlagFile}"
}


def embedHexMerge3in1(target){
    // def bootFile = powershell(returnStdout: true, script: '$a = dir BOOT\\*.zip; $a.name').trim()
    // println "bootZip: ${target}\\${bootFile}"
    // unzip charset: "GBK", dir: "${target}", glob: "", zipFile: "${target}\\${bootFile}"
    def bootHexFile = powershell(returnStdout: true, script: '$a = dir OUT\\*BOOT\\*.hex; $a.name').trim()
    def bootDir = powershell(returnStdout: true, script: '$a = dir OUT\\*BOOT; $a.name').trim()
    def appHexFile = powershell(returnStdout: true, script: '$a = dir OUT\\*V???*\\*.hex; $a.name').trim()
    def appDir = powershell(returnStdout: true, script: '$a = dir OUT\\*V???*; $a.name').trim()
    def iapHexFile = powershell(returnStdout: true, script: '$a = dir OUT\\*IAP\\*.hex; $a.name').trim()
    def iapDir = powershell(returnStdout: true, script: '$a = dir OUT\\*IAP; $a.name').trim()

    println "=======================HEX INFO======================="
    // println "bootFile: " + bootFile
    println "bootHexFile: " + bootHexFile
    println "bootDir: " + bootDir
    println "appHexFile: " + appHexFile
    println "appDir: " + appDir
    println "iapHexFile: " + iapHexFile
    println "iapDir: " + iapDir
    println "=======================HEX INFO======================="

    // bat "xcopy ${target}\\${bootDir}\\${bootHexFile} OUT\\${appDir}"
    bat "python %hexMergeScript% OUT\\${bootDir}\\${bootHexFile} OUT\\${iapDir}\\${iapHexFile} OUT\\${appDir}\\${appHexFile} OUT\\${appDir}\\${appHexFile}"
    // bat "del OUT\\${appDir}\\${bootHexFile}"
    // bat "del OUT\\${appDir}\\${hexFlagFile}"
}

def embedHexMerge4in1(project){
    
    def hexMergeProfile = hexMergeProfile(project)
    hexMergeProfile.each{
        println 'project: ' + it.key
      	println 'info: ' + it.value
        def file = it.value
        def bootHexDir = file.get('BOOT')
        def bootHexFile = powershell(returnStdout: true, script: "\$a = dir ${bootHexDir}\\*.hex; \$a.name").trim()
        def iapHexDir = file.get('IAP')
        def iapHexFile = powershell(returnStdout: true, script: "\$a = dir ${iapHexDir}\\*.hex; \$a.name").trim()
        def flagHexDir = file.get('FLAG')
        def flagHexFile = powershell(returnStdout: true, script: "\$a = dir ${flagHexDir}\\*.hex; \$a.name").trim()
        def appHexDir = file.get('APP')
        def appHexFile = powershell(returnStdout: true, script: "\$a = dir ${appHexDir}\\*.hex; \$a.name").trim()
        println "=======================HEX INFO======================="
        println "bootHexDir: " + bootHexDir
        println "bootHexFile: " + bootHexFile
        println "iapHexDir: " + iapHexDir
        println "iapHexFile: " + iapHexFile
        println "flagHexDir: " + flagHexDir
        println "flagHexFile: " + flagHexFile
        println "appHexDir: " + appHexDir
        println "appHexFile: " + appHexFile
        println "=======================HEX INFO======================="
        
        println "python %hexMerge4in1Script% ${bootHexDir}\\${bootHexFile} ${iapHexDir}\\${iapHexFile} ${flagHexDir}\\${flagHexFile} ${appHexDir}\\${appHexFile} ${appHexDir}\\${appHexFile}"
        bat "python %hexMerge4in1Script% ${bootHexDir}\\${bootHexFile} ${iapHexDir}\\${iapHexFile} ${flagHexDir}\\${flagHexFile} ${appHexDir}\\${appHexFile} ${appHexDir}\\${appHexFile}"
    }

}

def embedHexMerge3in1Standard(project){
    
    def hexMergeProfile = hexMergeProfile(project)
    hexMergeProfile.each{
        println 'project: ' + it.key
      	println 'info: ' + it.value
        def file = it.value
        def bootHexDir = file.get('BOOT')
        def bootHexFile = powershell(returnStdout: true, script: "\$a = dir ${bootHexDir}\\*.hex; \$a.name").trim()
        def flagHexDir = file.get('FLAG')
        def flagHexFile = powershell(returnStdout: true, script: "\$a = dir ${flagHexDir}\\*.hex; \$a.name").trim()
        def appHexDir = file.get('APP')
        def appHexFile = powershell(returnStdout: true, script: "\$a = dir ${appHexDir}\\*.hex; \$a.name").trim()
        println "=======================HEX INFO======================="
        println "bootHexDir: " + bootHexDir
        println "bootHexFile: " + bootHexFile
        println "flagHexDir: " + flagHexDir
        println "flagHexFile: " + flagHexFile
        println "appHexDir: " + appHexDir
        println "appHexFile: " + appHexFile
        println "=======================HEX INFO======================="
        if(it.key == 'SI100_Ult_Ctrl'){
            println "python %hexMergeScript% ${bootHexDir}\\${bootHexFile} ${appHexDir}\\${appHexFile} ${flagHexDir}\\${flagHexFile} ${appHexDir}\\${appHexFile}"
            bat "python %hexMergeScript% ${bootHexDir}\\${bootHexFile} ${flagHexDir}\\${flagHexFile} ${appHexDir}\\${appHexFile} ${appHexDir}\\${appHexFile}"
        }else{
            println "python %hexMergeScript% ${bootHexDir}\\${bootHexFile} ${flagHexDir}\\${flagHexFile} ${appHexDir}\\${appHexFile} ${appHexDir}\\${appHexFile}"
            bat "python %hexMergeScript% ${bootHexDir}\\${bootHexFile} ${flagHexDir}\\${flagHexFile} ${appHexDir}\\${appHexFile} ${appHexDir}\\${appHexFile}"
        }
        
    }

}

def embedHexMergeStandard(project){

    def hexMergeProfile = hexMergeProfile(project)
    def bootHexDir = hexMergeProfile.get('BOOT')
    def bootHexFile = powershell(returnStdout: true, script: "\$a = dir ${bootHexDir}\\*.hex; \$a.name").trim()
    def flagHexDir = hexMergeProfile.get('FLAG')
    def flagHexFile = powershell(returnStdout: true, script: "\$a = dir ${flagHexDir}\\*.hexflag; \$a.name").trim()
    def appHexDir = hexMergeProfile.get('APP')
    def appHexFile = powershell(returnStdout: true, script: "\$a = dir ${appHexDir}\\*.hex; \$a.name").trim()
    println "=======================HEX INFO======================="
    println "bootHexDir: " + bootHexDir
    println "bootHexFile: " + bootHexFile
    println "flagHexDir: " + flagHexDir
    println "flagHexFile: " + flagHexFile
    println "appHexDir: " + appHexDir
    println "appHexFile: " + appHexFile
    println "=======================HEX INFO======================="

    println "python %hexMergeScript% ${bootHexDir}\\${bootHexFile} ${flagHexDir}\\${flagHexFile} ${appHexDir}\\${appHexFile} OUT\\${appHexFile}"
//    bat "python %hexMergeScript% ${bootHexDir}\\${bootHexFile} ${flagHexDir}\\${flagHexFile} ${appHexDir}\\${appHexFile} OUT\\${appHexFile}"
    bat "python C:\\Tools\\hex_merge_3in_common.py ${bootHexDir}\\${bootHexFile} ${flagHexDir}\\${flagHexFile} ${appHexDir}\\${appHexFile} OUT\\${appHexFile}"


}
def embedHexMergeStandard2(project){

    def hexMergeProfile = hexMergeProfile(project)
    def bootHexDir = hexMergeProfile.get('BOOT')
    def bootHexFile = powershell(returnStdout: true, script: "\$a = dir ${bootHexDir}\\*.hex; \$a.name").trim()
    def flagHexDir = hexMergeProfile.get('FLAG')
    def flagHexFile = powershell(returnStdout: true, script: "\$a = dir ${flagHexDir}\\*.hexflag; \$a.name").trim()
    def appHexDir = hexMergeProfile.get('APP')
    def appHexFile = powershell(returnStdout: true, script: "\$a = dir ${appHexDir}\\*.hex; \$a.name").trim()
    println "=======================HEX INFO======================="
    println "bootHexDir: " + bootHexDir
    println "bootHexFile: " + bootHexFile
    println "flagHexDir: " + flagHexDir
    println "flagHexFile: " + flagHexFile
    println "appHexDir: " + appHexDir
    println "appHexFile: " + appHexFile
    println "=======================HEX INFO======================="

    println "python %hexMergeScript% ${bootHexDir}\\${bootHexFile} ${appHexDir}\\${appHexFile} OUT\\${appHexFile}"
//    bat "python %hexMergeScript% ${bootHexDir}\\${bootHexFile} ${appHexDir}\\${appHexFile} OUT\\${appHexFile}"
    bat "python C:\\Tools\\hex_merge_3in_common.py ${bootHexDir}\\${bootHexFile} ${flagHexDir}\\${flagHexFile} ${appHexDir}\\${appHexFile} OUT\\${appHexFile}"


}